package com.bb.blog.activiti.config;

import com.bb.blog.activiti.config.listener.MyActivitiEventListener;
import lombok.RequiredArgsConstructor;
import org.activiti.engine.*;
import org.activiti.engine.delegate.event.ActivitiEventType;
import org.activiti.engine.impl.asyncexecutor.DefaultAsyncJobExecutor;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * Activiti 引擎配置类
 *
 * @author czb
 */
@Configuration
@RequiredArgsConstructor
public class ActivitiEngineConfig {

    private final DataSource dataSource;
    private final PlatformTransactionManager platformTransactionManager;
    private Logger logger = LoggerFactory.getLogger(ActivitiEngineConfig.class);


    @Bean
    public SpringProcessEngineConfiguration springProcessEngineConfiguration(DefaultAsyncJobExecutor defaultAsyncJobExecutor) {

        SpringProcessEngineConfiguration spec = new SpringProcessEngineConfiguration();
        /* 设置数据源 */
        spec.setDataSource(dataSource);
        /* 设置事务管理 */
        spec.setTransactionManager(platformTransactionManager);
        /* 设置数据库更新策略 */
        spec.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        /* 设置命令拦截器 */
//        spec.setCustomPreCommandInterceptors(Collections.singletonList(new MyPreInterceptor()));
        /*设置命令拦截器*/
//        spec.setCustomPostCommandInterceptors(Collections.singletonList(new MyPostInterceptor()));


        /* 设置部署拦截器 用于设置动态任务之类的 */
//        spec.setPreBpmnParseHandlers(Collections.singletonList(new UserTaskAssignParseHandler()));
        /* single-resource or  default  */
//        spec.setDeploymentMode("single-resource");

//        Resource[] resources = null;
//        // 启动自动部署流程
//        try {
//            resources = new PathMatchingResourcePatternResolver().getResources("classpath*:processes/*.bpmn");
//        } catch (IOException e) {
//            logger.error("流程引擎初始化错误", e);
//        }
//        //流程资源的自动部署
//        spec.setDeploymentResources(resources);

        spec.setCreateDiagramOnDeploy(true);

        //设置ProcessDefinition缓存容量默认为不限制
        spec.setProcessDefinitionCacheLimit(10);

        /* 设置异步任务 */
//        spec.setAsyncExecutor(defaultAsyncJobExecutor);
//        spec.setAsyncExecutorActivate(true);
        return spec;
    }

    @Bean
    public ProcessEngineFactoryBean processEngine(SpringProcessEngineConfiguration configuration) {
        ProcessEngineFactoryBean processEngineFactoryBean = new ProcessEngineFactoryBean();
        processEngineFactoryBean.setProcessEngineConfiguration(configuration);
        return processEngineFactoryBean;
    }

    @Bean
    public RepositoryService repositoryService(ProcessEngine processEngine) throws Exception {
        return processEngine.getRepositoryService();
    }

    @Bean
    public RuntimeService runtimeService(ProcessEngine processEngine) throws Exception {
        RuntimeService runtimeService = processEngine.getRuntimeService();
        runtimeService.addEventListener(new MyActivitiEventListener(), ActivitiEventType.ACTIVITY_STARTED);
        return runtimeService;
    }

    @Bean
    public TaskService taskService(ProcessEngine processEngine) throws Exception {
        return processEngine.getTaskService();
    }

    @Bean
    public HistoryService historyService(ProcessEngine processEngine) throws Exception {
        return processEngine.getHistoryService();
    }

    @Bean
    public DynamicBpmnService dynamicBpmnService(ProcessEngine processEngine) throws Exception {

        return processEngine.getDynamicBpmnService();
    }
    @Bean ManagementService managementService(ProcessEngine processEngine) throws  Exception{
        return processEngine.getManagementService();
    }
}
